﻿#region Disclaimer/License Info

/* *********************************************** */

// sBlog.Net

// sBlog.Net is a minimalistic blog engine software.

// Homepage: http://sblogproject.net
// Github: http://github.com/karthik25/sBlog.Net

// This project is licensed under the BSD license.  
// See the License.txt file for more information.

/* *********************************************** */

#endregion
using System.Data.Linq;
using System.Linq;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Domain.Concrete
{
    public class Settings : ISettings
    {
        private Table<SettingsEntity> BlogSettings { get; set; }
        private readonly DataContext _context;
        private readonly string _connectionString;

        public Settings()
        {
            _connectionString = ApplicationDomainConfiguration.ConnectionString;
            _context = new DataContext(_connectionString);
            LoadSettings();
        }

        public string BlogName
        {
            get
            {                
                return "Mlog";
            }
            set
            {
                UpdateSettings("BlogName", "Mlog");
            }
        }

        public string BlogCaption
        {
            get
            {                
                return "Mlog - Nhật ký giải trí của bạn";
            }
            set
            {             
                UpdateSettings("BlogCaption", "Mlog - Nhật ký giải trí của bạn");
            }
        }

        public int BlogPostsPerPage
        {
            get
            {             
                return 10;
            }
            set
            {
                UpdateSettings("BlogPostsPerPage", "10");
            }
        }

        public string BlogTheme
        {
            get
            {             
                return "Mlog";
            }
            set
            {             
                UpdateSettings("BlogTheme", "Mlog");
            }
        }

        public bool BlogSocialSharing
        {
            get
            {
                return true;
            }
            set
            {
                UpdateSettings("BlogSocialSharing", value.ToString());
            }
        }

        public bool BlogSyntaxHighlighting
        {
            get
            {
                return false;
            }
            set
            {
                UpdateSettings("BlogSyntaxHighlighting", value.ToString());
            }
        }

        public string BlogSyntaxTheme
        {
            get
            {             
                return "";
            }
            set
            {             
                UpdateSettings("BlogSyntaxTheme", "");
            }
        }

        public string BlogSyntaxScripts
        {
            get
            {             
                return "";
            }
            set
            {             
                UpdateSettings("BlogSyntaxScripts", "");
            }
        }

        public bool BlogAkismetEnabled
        {
            get
            {
                return false;
            }
            set
            {
                UpdateSettings("BlogAkismetEnabled", value.ToString());
            }
        }

        public string BlogAkismetKey
        {
            get
            {             
                return "";
            }
            set
            {             
                UpdateSettings("BlogAkismetKey", "");
            }
        }

        public string BlogAkismetUrl
        {
            get
            {             
                return "http://mlog.yan.vn";
            }
            set
            {             
                UpdateSettings("BlogAkismetUrl", "http://mlog.yan.vn");
            }
        }

        public bool BlogAkismetDeleteSpam
        {
            get
            {             
                return false;
            }
            set
            {
                UpdateSettings("BlogAkismetDeleteSpam", value.ToString());
            }
        }

        public int BlogSocialSharingChoice
        {
            get
            {             
                return 3;
            }
            set
            {
                UpdateSettings("BlogSocialSharingChoice", value.ToString());
            }
        }

        public bool BlogSiteErrorEmailAction
        {
            get
            {             
                return false;
            }
            set
            {
                UpdateSettings("BlogSiteErrorEmailAction", value.ToString());
            }
        }

        public string BlogAdminEmailAddress
        {
            get
            {             
                return "philvaptech@hotmail.com";
            }
            set
            {                
                UpdateSettings("BlogAdminEmailAddress", "philvaptech@hotmail.com");
            }
        }

        public string BlogSmtpAddress
        {
            get
            {             
                return "smtp.live.com";
            }
            set
            {             
                UpdateSettings("BlogSmtpAddress", "smtp.live.com");
            }
        }

        public string BlogSmtpPassword
        {
            get
            {             
                return "QOVpSLEUS6x+ARiK0K5THwkj0/CA23mU";
            }
            set
            {             
                UpdateSettings("BlogSmtpPassword", "QOVpSLEUS6x+ARiK0K5THwkj0/CA23mU");
            }
        }

        public bool InstallationComplete
        {
            get
            {             
                return true;
            }
            set
            {
                UpdateSettings("InstallationComplete", value.ToString());
            }
        }

        public int ManageItemsPerPage
        {
            get
            {             
                return 15;
            }
            set
            {
                UpdateSettings("ManageItemsPerPage", value.ToString());
            }
        }

        public bool DisqusEnabled
        {
            get
            {             
                return false;
            }
            set
            {
                UpdateSettings("DisqusEnabled", value.ToString());
            }
        }

        public string BlogDisqusShortName
        {
            get
            {             
                return "";
            }
            set
            {             
                UpdateSettings("BlogDisqusShortName", "");
            }
        }

        public string BlogDbVersion
        {
            get
            {             
                return "1.0";
            }
            set
            {             
                UpdateSettings("BlogDbVersion", "1.0");
            }
        }

        public string GetValue(string key)
        {
            string value = string.Empty;
         //   string obj = DistCache.Get<string>(MemCacheKey.KeySettingGetValue + key);
          //  if (obj == null)
          //  {
                var setting = BlogSettings.SingleOrDefault(s => s.KeyName == key);
                if (setting != null)
                    value = setting.KeyValue;
            //    DistCache.Add(MemCacheKey.KeySettingGetValue + key, value);
            //}
            //else
            //{
              //  value = obj;
            //}
            return value;
        }

        public bool UpdateSettings(string key, string value)
        {
            var setting = BlogSettings.Single(s => s.KeyName == key);
            if (setting != null)
            {
                setting.KeyValue = value;
                _context.SubmitChanges();
                return true;
            }
            return false;
        }

        private void LoadSettings()
        {
            BlogSettings = _context.GetTable<SettingsEntity>();
        }

        private static string GetValueInternal(string value)
        {
            var valueInternal = value;
            if (value == null || value.Trim() == string.Empty)
                valueInternal = null;
            return valueInternal;
        }
    }
}
